package com.mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.collision.PhysicsCollisionEvent;
import com.jme3.bullet.collision.PhysicsCollisionListener;
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.input.InputManager;
import com.jme3.input.KeyInput;
import static com.jme3.input.KeyInput.KEY_SPACE;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;

/**
 * This is the Main Class of your Game. You should only do initialization here.
 * Move your Logic into AppStates or Controls
 * @author normenhansen
 */
public class Main extends SimpleApplication  implements PhysicsCollisionListener, ActionListener {

  
    
    public static void main(String[] args) {
        Main app = new Main();
        app.start();
    }
    private RigidBodyControl PlayerBody;
    private boolean IsGround = false;
    @Override
    public void simpleInitApp() {
        Box b = new Box(1, 1, 1);
        Geometry geom = new Geometry("Player", b);

        Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mat.setColor("Color", ColorRGBA.Blue);
        geom.setMaterial(mat);
        
          CollisionShape BoxShape = new BoxCollisionShape(new Vector3f(1,1,1));
        RigidBodyControl boxRigidBody = new RigidBodyControl(BoxShape,1f);
        geom.addControl(boxRigidBody);
        BulletAppState bulletAppState = new BulletAppState();
        stateManager.attach(bulletAppState);
        PhysicsSpace physicsSpace = bulletAppState.getPhysicsSpace();
        
        

       
        
        Box b1 = new Box(1, 1, 1);
        Geometry geom1 = new Geometry("Box2", b1);
        Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mat1.setColor("Color", ColorRGBA.Blue);
        geom1.setMaterial(mat);
        geom1.setLocalTranslation(new Vector3f(0,-20,0));
        
        CollisionShape BoxShape1 = new BoxCollisionShape(new Vector3f(1,1,1));
        RigidBodyControl boxRigidBody1 = new RigidBodyControl(BoxShape1,0);
        geom.addControl(boxRigidBody1);
  
        

        
       
        physicsSpace.add(boxRigidBody);
        physicsSpace.add(boxRigidBody1);
        
        physicsSpace.addCollisionListener(this);
        
        
        rootNode.attachChild(geom);
        rootNode.attachChild(geom1);
         
        inputManager.addMapping("Jump", new KeyTrigger(KeyInput.KEY_SPACE));
        inputManager.addListener(this,"Jump");
                
    }
    
    @Override
    public void collision(PhysicsCollisionEvent event)
    {
       System.out.println(event);
       System.out.println("Н"+ event.getNodeA().getName()+"И"+ event.getNodeB().getName());
       if (event.getNodeA().getName().equals("Player") && event.getNodeB().getName().equals("Box2"))
           
          
       {
             IsGround = true;
             System.out.println(IsGround);
        }               
           if (event.getNodeA().getName().equals("blueBox")&&
            event.getNodeB().getName().equals("RedPlatform")){
               event.getNodeA().setLocalScale(new Vector3f(3,3,3));
           }
       }
    
    
    @Override
    public void simpleUpdate(float tpf) {
        //TODO: add update code
    }

    @Override
    public void simpleRender(RenderManager rm) {
        //TODO: add render code
    }

    @Override
    public void onAction(String string, boolean bln, float f) {
     
    }
 
      
}
